/**
 * 
 */
package br.com.lazyprogrammer.social.infestit.system.generator;

import com.twmacinta.util.MD5;

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Random;

/**
 * 
 * @author Limao Games <br />
 * <b>Email:</b> andreikeda@limaogames.com.br <br />
 * created on: 07/05/2014
 * @version 1.0
 * @see 
 */
public class Generator {
	
	/**
	 * 
	 * @return
	 */
	public static String createKey(int n) {
		StringBuilder key = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < n; i++) {
			int r = random.nextInt(62);
			key.append(getChar(r));
		}
		
		return key.toString();
	}
	
	public static String encodeCustomId(String text, long dateOnMillis, Locale locale) {
		return toMD5(text + dateOnMillis, locale);
	}
	
	public static String encodeId(String username, String email, Locale locale) {
		return toMD5(username + email, locale);
	}
	
	public static String encodePassword(String text, Locale locale) {
		return toMD5(text, locale);
	}
	
	/**
	 * 
	 * @param index
	 * @return
	 */
	private static String getChar(int index) {
		if (index < 10)
			return String.valueOf(index);
		switch(index) {
			case 10:
				return "a";
			case 11:
				return "b";
			case 12:
				return "c";
			case 13:
				return "d";
			case 14:
				return "e";
			case 15:
				return "f";
			case 16:
				return "g";
			case 17:
				return "h";
			case 18:
				return "i";
			case 19:
				return "j";
			case 20:
				return "k";
			case 21:
				return "l";
			case 22:
				return "m";
			case 23:
				return "n";
			case 24:
				return "o";
			case 25:
				return "p";
			case 26:
				return "q";
			case 27:
				return "r";
			case 28:
				return "s";
			case 29:
				return "t";
			case 30:
				return "u";
			case 31:
				return "v";
			case 32:
				return "w";
			case 33:
				return "x";
			case 34:
				return "y";
			case 35:
				return "z";
			case 36:
				return "A";
			case 37:
				return "B";
			case 38:
				return "C";
			case 39:
				return "D";
			case 40:
				return "E";
			case 41:
				return "F";
			case 42:
				return "G";
			case 43:
				return "H";
			case 44:
				return "I";
			case 45:
				return "J";
			case 46:
				return "K";
			case 47:
				return "L";
			case 48:
				return "M";
			case 49:
				return "N";
			case 50:
				return "O";
			case 51:
				return "P";
			case 52:
				return "Q";
			case 53:
				return "R";
			case 54:
				return "S";
			case 55:
				return "T";
			case 56:
				return "U";
			case 57:
				return "V";
			case 58:
				return "W";
			case 59:
				return "X";
			case 60:
				return "Y";
			case 61:
				return "Z";
		}
		return null;
	}

	/**
	 * 
	 * @param arg
	 * @return
	 */
	private static String toMD5(String arg, Locale locale) {
		if (arg.equals(""))
			return "";
		MD5 md5 = new MD5();
        try {
			md5.Update(arg, null);
	        String hash = md5.asHex();
	        return hash.toUpperCase(locale);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}

}
